package com.ruoyi.system.service.impl;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.domain.dto.LuntaiPolicyExtensionRequestDto;
import com.ruoyi.system.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.service.ILuntaiPolicyExtensionRequestService;

/**
 * 保单延保申请记录Service业务层处理
 *
 * @author ruoyi
 * @date 2025-02-17
 */
@Service
public class LuntaiPolicyExtensionRequestServiceImpl implements ILuntaiPolicyExtensionRequestService {
    @Autowired
    private LuntaiPolicyExtensionRequestMapper luntaiPolicyExtensionRequestMapper;

    @Autowired
    private LuntaiPolicyRequestResultMapper luntaiPolicyRequestResultMapper;

    @Autowired
    private LuntaiMessageMapper luntaiMessageMapper;

    @Autowired
    private LuntaiOrderMapper luntaiOrderMapper;

    @Autowired
    private LuntaiEquipmentUseLogMapper equipmentUseLogMapper;



    /**
     * 查询保单延保申请记录
     *
     * @param id 保单延保申请记录主键
     * @return 保单延保申请记录
     */
    @Override
    public LuntaiPolicyExtensionRequest selectLuntaiPolicyExtensionRequestById(Integer id) {
        return luntaiPolicyExtensionRequestMapper.selectLuntaiPolicyExtensionRequestById(id);
    }

    /**
     * 查询保单延保申请记录列表
     *
     * @param luntaiPolicyExtensionRequest 保单延保申请记录
     * @return 保单延保申请记录
     */
    @Override
    public List<LuntaiPolicyExtensionRequest> selectLuntaiPolicyExtensionRequestList(LuntaiPolicyExtensionRequest luntaiPolicyExtensionRequest) {
        return luntaiPolicyExtensionRequestMapper.selectLuntaiPolicyExtensionRequestList(luntaiPolicyExtensionRequest);
    }

    @Override
    public List<LuntaiPolicyExtensionRequestDto> selectLuntaiPolicyExtensionRequestList2(LuntaiPolicyExtensionRequestDto luntaiPolicyExtensionRequestDto) {
        return luntaiPolicyExtensionRequestMapper.selectLuntaiPolicyExtensionRequestList2(luntaiPolicyExtensionRequestDto);
    }

    @Override
    public List<LuntaiPolicyExtensionRequestDto> selectLuntaiPolicyExtensionRequestList3(LuntaiPolicyExtensionRequestDto luntaiPolicyExtensionRequestDto) {
        return luntaiPolicyExtensionRequestMapper.selectLuntaiPolicyExtensionRequestList3(luntaiPolicyExtensionRequestDto);
    }

    /**
     * 新增保单延保申请记录
     *
     * @param luntaiPolicyExtensionRequest 保单延保申请记录
     * @return 结果
     */
    @Override
    public int insertLuntaiPolicyExtensionRequest(LuntaiPolicyExtensionRequest luntaiPolicyExtensionRequest) {
        luntaiPolicyExtensionRequest.setCreateTime(DateUtils.getNowDate());
        return luntaiPolicyExtensionRequestMapper.insertLuntaiPolicyExtensionRequest(luntaiPolicyExtensionRequest);
    }

    /**
     * 修改保单延保申请记录
     *
     * @param luntaiPolicyExtensionRequest 保单延保申请记录
     * @return 结果
     */
    @Override
    public int updateLuntaiPolicyExtensionRequest(LuntaiPolicyExtensionRequest luntaiPolicyExtensionRequest) {
        Integer status = luntaiPolicyExtensionRequest.getStatus();
        if (status != null && status != 0) {//如果不是更新状态的话
            Integer id = luntaiPolicyExtensionRequest.getId();
            LuntaiPolicyExtensionRequest request = luntaiPolicyExtensionRequestMapper.selectLuntaiPolicyExtensionRequestById(id);
            System.out.println(request);
            Integer requestId = luntaiPolicyExtensionRequest.getId();
            createPolicyResult(luntaiPolicyExtensionRequest);
            sendMessage(luntaiPolicyExtensionRequest,request);
            if (luntaiPolicyExtensionRequest.getStatus() == 3) {
                luntaiOrderMapper.cancelOrder(requestId, luntaiPolicyExtensionRequest.getType(), Math.toIntExact(SecurityUtils.getUserId()));
            } else if (luntaiPolicyExtensionRequest.getStatus() == 2) {
                updateOrderStatus(luntaiPolicyExtensionRequest,request);
            }
        }
        return luntaiPolicyExtensionRequestMapper.updateLuntaiPolicyExtensionRequest(luntaiPolicyExtensionRequest);
    }

    public void updateOrderStatus(LuntaiPolicyExtensionRequest luntaiPolicyExtensionRequest,LuntaiPolicyExtensionRequest request) {
        Integer requestId = luntaiPolicyExtensionRequest.getId();
        BigDecimal amount = equipmentUseLogMapper.getEquipmentTotalAmount(requestId);
        luntaiOrderMapper.editOrder(requestId,request.getType(),amount,luntaiPolicyExtensionRequest.getStatus(), Math.toIntExact(SecurityUtils.getUserId()));
    }

    public void createPolicyResult(LuntaiPolicyExtensionRequest luntaiPolicyExtensionRequest) {
        LuntaiPolicyRequestResult result = new LuntaiPolicyRequestResult();
        result.setAdminId(Math.toIntExact(SecurityUtils.getUserId()));
        result.setUserId(luntaiPolicyExtensionRequest.getUserId());
        result.setResult(luntaiPolicyExtensionRequest.getStatus());
        result.setRequestId(luntaiPolicyExtensionRequest.getId());
        result.setCreateTime(new Date());
        result.setIsdelete(0);
        luntaiPolicyRequestResultMapper.insertLuntaiPolicyRequestResult(result);
    }

    public void sendMessage(LuntaiPolicyExtensionRequest luntaiPolicyExtensionRequest,LuntaiPolicyExtensionRequest request) {
        LuntaiMessage luntaiMessage = new LuntaiMessage();
        luntaiMessage.setType(1);
        String result = "";
        String type = "";
        // 假设 getCreateTime() 返回的是 Date 类型
        Date createTime = request.getCreateTime();

        // 创建 SimpleDateFormat 实例，设置日期格式
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
        System.out.println(sdf);
        switch (request.getType()) {
            case 1:
                type = "延保";
                break;
            case 2:
                type = "鼓包";
                break;
        }
        luntaiMessage.setTitle("您发起的" + type + "申请管理员已处理");
        // 格式化时间
        String formattedTime = sdf.format(createTime);
        switch (luntaiPolicyExtensionRequest.getStatus()) {
            case 1:
                result = "已审核";
                break;
            case 2:
                result = "已完成";
                break;
            case 3:
                result = "已驳回";
                break;
        }
        luntaiMessage.setInfo("您于" + formattedTime + "提交的" + type + "申请管理员已处理，当前结果：" + result);
        luntaiMessage.setUserId(request.getUserId());
        luntaiMessage.setStoreId(request.getStoreId());
        luntaiMessage.setIsread(0);
        luntaiMessage.setIsdelete(0);
        luntaiMessageMapper.insertLuntaiMessage(luntaiMessage);
    }


    /**
     * 批量删除保单延保申请记录
     *
     * @param ids 需要删除的保单延保申请记录主键
     * @return 结果
     */
    @Override
    public int deleteLuntaiPolicyExtensionRequestByIds(Integer[] ids) {
        return luntaiPolicyExtensionRequestMapper.deleteLuntaiPolicyExtensionRequestByIds(ids);
    }

    /**
     * 删除保单延保申请记录信息
     *
     * @param id 保单延保申请记录主键
     * @return 结果
     */
    @Override
    public int deleteLuntaiPolicyExtensionRequestById(Integer id) {
        return luntaiPolicyExtensionRequestMapper.deleteLuntaiPolicyExtensionRequestById(id);
    }


}
